package gui.menu;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import command.CommandBroadcaster;
import command.ServerReader;

import xmlrpc.client.ClientUtil;
import core.Constants;
import core.Core;
import core.Player;
import core.Constants.ConnectionType;

/**
 * Set connection data and connect to server?
 * 
 * @author Qw4z1
 */

@SuppressWarnings("serial")
public class ConnectionDialog extends javax.swing.JDialog {
	private JButton jButtonCancel;
	private JButton jButtonOk;
	private JLabel jLabel1;
	private JLabel jLabel2;
	private JPanel jPanel1;
	private JPanel jPanel2;
	private JTextField jTextFieldServerIp;
	private JTextField jTextFieldServerPort;

	public ConnectionDialog(JFrame parent) {
		super(parent, "Connect", true);
		initComponents();
		this.setLocationRelativeTo(parent);
	}

	private void jButtonOkActionPerformed(java.awt.event.ActionEvent evt) {
		try {
			ClientUtil.getInstance().newConnectionOptions(
					jTextFieldServerIp.getText(),
					Integer.parseInt(jTextFieldServerPort.getText()));
			
			if(CommandBroadcaster.pingServer() == "ping"){
				JOptionPane.showMessageDialog(null, "Server successfully pinged!");
				Constants.setConnectionState(ConnectionType.CONNECTED);
				int playerId = CommandBroadcaster.getPlayerId();
				Player thisPlayer = Core.getInstance().getPlayers().get(playerId);
				Core.getInstance().setThisPlayer(thisPlayer);
				
				ServerReader.getInstance().startTimer();
				this.dispose();
			}
		} catch (NumberFormatException nfe) {
			JOptionPane.showMessageDialog(null, "No IP or port entered!");
		}
	}

	private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {
		this.dispose();
	}

	private void initComponents() {
		jPanel1 = new javax.swing.JPanel();
		jLabel1 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		jTextFieldServerIp = new javax.swing.JTextField();
		jTextFieldServerPort = new javax.swing.JTextField();
		jPanel2 = new javax.swing.JPanel();
		jButtonOk = new javax.swing.JButton();
		jButtonCancel = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		setTitle("Connect");

		// panel for the textfields
		jPanel1.setBorder(javax.swing.BorderFactory
				.createTitledBorder("Server"));
		jLabel1.setText("IP");
		jLabel2.setText("Port");

		// panel layout
		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
				jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout
				.setHorizontalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																jLabel1,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																51,
																Short.MAX_VALUE)
														.addComponent(jLabel2))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING,
																false)
														.addComponent(
																jTextFieldServerPort)
														.addComponent(
																jTextFieldServerIp,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																111,
																Short.MAX_VALUE))
										.addContainerGap(42, Short.MAX_VALUE)));
		jPanel1Layout
				.setVerticalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																jLabel1,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																23,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																jTextFieldServerIp,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel2)
														.addComponent(
																jTextFieldServerPort,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));

		// ok button
		jButtonOk.setText("OK");
		jButtonOk.addActionListener(new java.awt.event.ActionListener() {

			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButtonOkActionPerformed(evt);
			}
		});
		// cancel button
		jButtonCancel.setText("Cancel");
		jButtonCancel.addActionListener(new java.awt.event.ActionListener() {

			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButtonCancelActionPerformed(evt);
			}
		});
		// button layout
		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								javax.swing.GroupLayout.Alignment.TRAILING,
								layout
										.createSequentialGroup()
										.addGap(37, 37, 37)
										.addComponent(
												jButtonOk,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												71,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED,
												42, Short.MAX_VALUE)
										.addComponent(jButtonCancel).addGap(37,
												37, 37))
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.TRAILING)
														.addComponent(
																jPanel2,
																javax.swing.GroupLayout.Alignment.LEADING,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE)
														.addComponent(
																jPanel1,
																javax.swing.GroupLayout.Alignment.LEADING,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addContainerGap()));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(
												jPanel1,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
												jPanel2,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addGap(27, 27, 27)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																jButtonCancel)
														.addComponent(jButtonOk))
										.addContainerGap(21, Short.MAX_VALUE)));

		pack();
	}

}